# -*- coding:utf-8 -*-
import requests
# pip install pyexecjs
import execjs
import time

# 百度翻译逆向

# 有点问题，有时会报995错误，有时是1022
class TranslateData(object):
    def __init__(self):
        self.name = input("请输入需要翻译的内容：")
        self.url = "https://fanyi.baidu.com/v2transapi?from=zh&to=en"
        self.ip_url = "https://api.hailiangip.com:8522/api/getIpEncrypt?dataType=0&encryptParam=SlDyzgfgDW12vuaMHmQkMz9pKEmWH7kDAoD1ZC4KkxpHXuvLm%2B3L9xWaasGPtq4Tl%2F0mwvZ8ZXyhyzM1%2BSxXiaP4RAZjpPmXK84ycqmf2Ywxskuqxtlzs4Q2gz4R31nrmJ79s0RtMUHE%2FlQ6k3SGH5X6gWIUjjBESnvVgqGkB%2FtYDcvC%2FeTFVDIQgCd6Bl9z0AOtabf92lu9Nd5cdhcIj9TXdJz%2BFe7XNmfGghK3pfs%3D"
        self.headers = {
            'Accept': '*/*',
            'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
            'Acs-Token': '1696960810580_1697036412105_nmGsYKXqQeKjfSS3p3mXGKWnV6BZ/vP4H7Fk+ldC/8dxLgVLHkhzoP1tBUex+Ckb1rDZcK6okI4hoMl089t0Bv69khEgnED2I2y3eJvjDXKndkyw5HTsokoPbPoni3tsY+G7QXA4lwu9ONAMdX9QxjwUIkBxm1bOC3VGvrztKxKfz+dSvk+cn7QBg7oY8Tmak/0nXvlXnrdYFxUXsyJVOxbXE/V0x81Ijf9XMDeSRJJF4MWjUkyFlG7wnQD23RYuQF+r2+wlRJjNqZN/u1SLPAGUmTH+rlqdADNK9+yk+tMxhrCKcaNg4KZ2qL/9LlctfPRYdmvHW3fTBvOzhrzyHbryx2MS7oBCVqCM2UJzBhV2saYejGUVJeqXCLCJyUPqnZWUEqTZU5pE3B6rr3PT44LB7R8J65/3vFK+w7VlwxIt/6Aq0H4sAGRBv6Gc96isIkSAIpwhBHbSl3QlTuTPyeo52lwxm1Jd5Z6rqpQxJPACrH2Psa//y5GCMl9T/psUmFmS6YqAzz2UtKU8/j5csQ==',
            'Connection': 'keep-alive',
            'Content-Length': '166',
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
            'Cookie': 'BAIDUID=41B7D0E7FD164B7DC20D7AF82EFEAEA5:FG=1; H_WISE_SIDS=219946_131862_216837_213354_214805_219942_213039_204915_110085_236308_243886_244476_244718_240590_245412_247148_250889_251428_249892_254592_253426_255981_256083_255661_254835_256739_251973_256620_257081_257102_254075_257289_254317_251058_257661_257936_257167_257904_257823_257586_253900_258253_257994_255224_258376_227147_258723_258728_258938_257302_258985_258958_258694_230288_259047_257016_107316_252256_259191_259193_259277_256223_259403_259286_259391_259431_259536_228132_259569_259605_259628_259581_256998_259558_259653_258773_234296_234208_259888_259642_255909_260045_254300_260125_259958_260235_256229_260355_260390_260362_259187_260457_253022_255212_260569_258081_260334_259408_260651_260352_251786_253676_260834_259312_259422_259583_260718_260976_260938_259031; BIDUPSID=41B7D0E7FD164B7DC20D7AF82EFEAEA5; PSTM=1684829720; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; H_WISE_SIDS_BFESS=219946_131862_216837_213354_214805_219942_213039_204915_110085_236308_243886_244476_244718_240590_245412_247148_250889_251428_249892_254592_253426_255981_256083_255661_254835_256739_251973_256620_257081_257102_254075_257289_254317_251058_257661_257936_257167_257904_257823_257586_253900_258253_257994_255224_258376_227147_258723_258728_258938_257302_258985_258958_258694_230288_259047_257016_107316_252256_259191_259193_259277_256223_259403_259286_259391_259431_259536_228132_259569_259605_259628_259581_256998_259558_259653_258773_234296_234208_259888_259642_255909_260045_254300_260125_259958_260235_256229_260355_260390_260362_259187_260457_253022_255212_260569_258081_260334_259408_260651_260352_251786_253676_260834_259312_259422_259583_260718_260976_260938_259031; APPGUIDE_10_6_2=1; APPGUIDE_10_6_5=1; MCITY=-%3A; BAIDUID_BFESS=41B7D0E7FD164B7DC20D7AF82EFEAEA5:FG=1; ZFY=o:BqOmgSMiP0dt9N1AFVKCa3VB0rMLFbcJ5cbjeo8DYY:C; BA_HECTOR=0k81ah8lak8h20ak802hal811iicdi41p; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; APPGUIDE_10_6_6=1; delPer=0; PSINO=7; H_PS_PSSID=39314_39398_39392_39420_39413_39480_39467_39234_39403_39487_26350_39430; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1697035006,1697035171,1697036890,1697083121; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1697083121; ab_sr=1.0.1_ZGU0NjdkZjZjOWJjMzEzZmUyYWQ0NTM1OTE4ODE5MzNiYzAzZDc4ZDA3YjlmMTc5NGQzYzNjYzE4NThhNGIzZTMyMTkxNWUyYWNkY2M5Njc3MmY0NTVmMDY1YTgzOWE1ODU0OTNkYTg4N2YyNTliZmRhNTc1MDlkYzYwZmFkM2YzMjQ4ZDNlNDAwZTIzOTdjNTkyMDZmZWRmMmZiZjQ0Yw==',
            'Host': 'fanyi.baidu.com',
            'Origin': 'https://fanyi.baidu.com',
            'Referer': 'https://fanyi.baidu.com/',
            'Sec-Ch-Ua': '"Microsoft Edge";v="117", "Not;A=Brand";v="8", "Chromium";v="117"',
            'Sec-Ch-Ua-Mobile': '?0',
            'Sec-Ch-Ua-Platform': '"Windows"',
            'Sec-Fetch-Dest': 'empty',
            'Sec-Fetch-Mode': 'cors',
            'Sec-Fetch-Site': 'same-origin',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.60',
            'X-Requested-With': 'XMLHttpRequest',
        }

    # 构建代理
    def get_ip(self):
        ip_resp = requests.get(self.ip_url).json()
        proxy = {
            "https:": "https://" + str(ip_resp["data"][0]["ip"]) + ":" + str(ip_resp["data"][0]["port"])
        }
        return proxy


    def get_sign(self):
        content = open("trans.js", "r", encoding="utf-8").read()
        # 编译加载js内容
        data_js = execjs.compile(content)
        sign = data_js.call("trans", self.name)
        return sign

    # 目标网站，发起post请求
    def get_data_index(self, proxy, sign):
        time.sleep(3)
        data = {
            'from': 'zh',
            'to': 'en',
            'query': self.name,
            'transtype': 'realtime',
            'simple_means_flag': '3',
            'sign': sign,
            'token': 'deae1aa1fdbabdcb505c1cfc0f88c51d',
            'domain': 'common',
            'ts': '1697025707877'
        }
        response = requests.post(self.url, data=data, headers=self.headers, proxies=proxy)
        print(response.json()['trans_result']["data"][0]["dst"])

    def main(self):
        proxy = self.get_ip()
        sign = self.get_sign()
        self.get_data_index(proxy, sign)

if __name__ == '__main__':
    spider = TranslateData()
    spider.main()

